﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebReference;
using System.Data.SqlClient;

public partial class Admin_OilPhysicalProperties : System.Web.UI.Page
{
    Service objService = new Service();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
        }
        else
        {
            string ExperimentId = (string)Session["ExperimentId"];
            if (ExperimentId != null)
            {
                lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;
            }

            string id = Request.QueryString["id"];

            if (id != null)
            {
                fillOilPhysicalProperties(Convert.ToInt32(id));
                txtOilPhysicalPropId.Text = string.Empty;
                trDgOilPhysicalProperties.Visible = false;
                trOilPhyTextBox.Visible = true;
            }

            BindDataGrid();
        }
    }
    private void BindDataGrid()
    {
        string ExpID = (string)Session["ExperimentId"];
        if (ExpID != null)
        {
            if (objService.GetOilPhysicalProperties(ExpID).Tables[0].Rows.Count > 0)
            {
                dgOilPhysicalProps.DataSource = objService.GetOilPhysicalProperties(ExpID);
                dgOilPhysicalProps.DataBind();
                trDgOilPhysicalProperties.Visible = true;
                trOilPhyTextBox.Visible = false;
            }
            else
            {
                trDgOilPhysicalProperties.Visible = false;
                trOilPhyTextBox.Visible = true;
            }

        }
        else
        {
            trDgOilPhysicalProperties.Visible = false;
            trOilPhyTextBox.Visible = true;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
    }
    protected void dgOilPhysicalProps_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(dgOilPhysicalProps.Rows[e.NewEditIndex].Cells[1].Text.Trim());
        fillOilPhysicalProperties(id);
        trDgOilPhysicalProperties.Visible = false;
        trOilPhyTextBox.Visible = true;
        btnPrevious.Visible = false;
    }

    private void fillOilPhysicalProperties(Int32 id)
    {
        DataSet ds = new DataSet();
        ds = objService.GetTableDetailsBasedonPK("oilphysprops", id);

        txtDensity.Text = ds.Tables[0].Rows[0]["density"].ToString();
        txtViscosity.Text = ds.Tables[0].Rows[0]["viscosity"].ToString();
        txtViscosityTemper.Text = ds.Tables[0].Rows[0]["viscosity_temp"].ToString();
        txtFlashPoint.Text = ds.Tables[0].Rows[0]["flashPoint"].ToString();
        txtPourpoint.Text = ds.Tables[0].Rows[0]["pourPoint"].ToString();
        txtPh.Text = ds.Tables[0].Rows[0]["pH"].ToString();
        txtHHV.Text = ds.Tables[0].Rows[0]["HHV"].ToString();
        txtHeatofCombution.Text = ds.Tables[0].Rows[0]["HeatOfCombustion"].ToString();
        txtWaterContenet.Text = ds.Tables[0].Rows[0]["WaterContent"].ToString();
        txtConradsenCarbon.Text = ds.Tables[0].Rows[0]["ConradsenCarbon"].ToString();
        txtOilPhysicalPropId.Text = ds.Tables[0].Rows[0]["id"].ToString();
    }

    private void ClearTextBoxes()
    { 
        txtDensity.Text = string.Empty ;
        txtViscosity.Text = string.Empty;
        txtViscosityTemper.Text = string.Empty;
        txtFlashPoint.Text = string.Empty;
        txtPourpoint.Text = string.Empty;
        txtPh.Text = string.Empty;
        txtHHV.Text = string.Empty;
        txtHeatofCombution.Text = string.Empty;
        txtWaterContenet.Text = string.Empty;
        txtConradsenCarbon.Text = string.Empty;
        txtOilPhysicalPropId.Text = string.Empty;
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (txtOilPhysicalPropId.Text != string.Empty)
        {
            if (txtDensity.Text == string.Empty)
            {
                lblMsg.Text = objService.UpdateTable("oilphysprops", "density", "null", txtOilPhysicalPropId.Text);
            }
            else
            {
                lblMsg.Text = objService.UpdateTable("oilphysprops", "density", txtDensity.Text, txtOilPhysicalPropId.Text);
            }

            if (txtViscosity.Text == string.Empty)
            {
                lblMsg.Text = objService.UpdateTable("oilphysprops", "viscosity", "null", txtOilPhysicalPropId.Text);
            }
            else
            {
                lblMsg.Text = objService.UpdateTable("oilphysprops", "viscosity", txtViscosity.Text, txtOilPhysicalPropId.Text);
            }

            if (txtViscosityTemper.Text == string.Empty)
            {
                lblMsg.Text = objService.UpdateTable("oilphysprops", "viscosity_temp", "null", txtOilPhysicalPropId.Text);
            }
            else
            {
                lblMsg.Text = objService.UpdateTable("oilphysprops", "viscosity_temp", txtViscosityTemper.Text, txtOilPhysicalPropId.Text);
            }

            if (txtFlashPoint.Text == string.Empty)
            {
                lblMsg.Text = objService.UpdateTable("oilphysprops", "flashPoint", "null", txtOilPhysicalPropId.Text);
            }
            else
            {
                lblMsg.Text = objService.UpdateTable("oilphysprops", "flashPoint", txtFlashPoint.Text, txtOilPhysicalPropId.Text);
            }

            if (txtPourpoint.Text == string.Empty)
            {
                lblMsg.Text = objService.UpdateTable("oilphysprops", "pourPoint", "null", txtOilPhysicalPropId.Text);
            }
            else
            {
                lblMsg.Text = objService.UpdateTable("oilphysprops", "pourPoint", txtPourpoint.Text, txtOilPhysicalPropId.Text);
            }
            if (txtPh.Text == string.Empty)
            {
                lblMsg.Text = objService.UpdateTable("oilphysprops", "pH", "null", txtOilPhysicalPropId.Text);
            }
            else
            {
                lblMsg.Text = objService.UpdateTable("oilphysprops", "pH", txtPh.Text, txtOilPhysicalPropId.Text);
            }
            if (txtHHV.Text == string.Empty)
            {
                lblMsg.Text = objService.UpdateTable("oilphysprops", "HHV", "null", txtOilPhysicalPropId.Text);
            }
            else
            {
                lblMsg.Text = objService.UpdateTable("oilphysprops", "HHV", txtHHV.Text, txtOilPhysicalPropId.Text);
            }

            if (txtHeatofCombution.Text == string.Empty)
            {
                lblMsg.Text = objService.UpdateTable("oilphysprops", "HeatOfCombustion", "null", txtOilPhysicalPropId.Text);
            }
            else
            {
                lblMsg.Text = objService.UpdateTable("oilphysprops", "HeatOfCombustion", txtHeatofCombution.Text, txtOilPhysicalPropId.Text);
            }

            if (txtWaterContenet.Text == string.Empty)
            {
                lblMsg.Text = objService.UpdateTable("oilphysprops", "WaterContent", "null", txtOilPhysicalPropId.Text);
            }
            else
            {
                lblMsg.Text = objService.UpdateTable("oilphysprops", "WaterContent", txtWaterContenet.Text, txtOilPhysicalPropId.Text);
            }

            if (txtConradsenCarbon.Text == string.Empty)
            {
                lblMsg.Text = objService.UpdateTable("oilphysprops", "ConradsenCarbon", "null", txtOilPhysicalPropId.Text);
            }
            else
            {
                lblMsg.Text = objService.UpdateTable("oilphysprops", "ConradsenCarbon", txtConradsenCarbon.Text, txtOilPhysicalPropId.Text);
            }

            BindDataGrid();

            ClearTextBoxes();
        }
        else
        {
            if ((string)Session["ExperimentId"] != null)
            {
                string ExpID = (string)Session["ExperimentId"];

                lblMsg.Text = objService.SaveOilPhysicalProperties(txtDensity.Text, txtViscosity.Text, txtViscosityTemper.Text, txtFlashPoint.Text, txtPourpoint.Text, txtPh.Text, txtHHV.Text, txtHeatofCombution.Text, txtWaterContenet.Text, txtConradsenCarbon.Text,ExpID);
                BindDataGrid();
                ClearTextBoxes();
            }
            else
            {
                string RefeId = (string)Session["ReferenceId"];

                objService.SaveExperiment(null, null, null, null, null, null, null, null, null, null, null, RefeId, null);

                string ExperimentId = objService.GetExperimentId("ref_fk", RefeId);

                //string ExperimentId = objService.GetLastInsertId().ToString();

                Session["ExperimentId"] = ExperimentId;

                lblMsg.Text = objService.SaveOilPhysicalProperties(txtDensity.Text, txtViscosity.Text, txtViscosityTemper.Text, txtFlashPoint.Text, txtPourpoint.Text, txtPh.Text, txtHHV.Text, txtHeatofCombution.Text, txtWaterContenet.Text, txtConradsenCarbon.Text, ExperimentId);

               

                BindDataGrid();

                ClearTextBoxes();

            }



            string OilPhysicalProId = objService.GetLastInsertedId("experiment_id", (string)Session["ExperimentId"], "oilphysprops");
                //GetOilPhysicalPropertiesRecentId();

            if ((string)Session["LiquidCompId"] != null)
            {
                string LiquidCompId = (string)Session["LiquidCompId"];
                lblMsg.Text = "Oil Physical properties Saved and " + objService.UpdateTable("liquidcomp", "oil_phys_fk", OilPhysicalProId, LiquidCompId);
            }
            else
            {
                lblMsg.Text = "Oil Physical properties Saved and " + objService.SaveLiquidComp(null, null, null, null, OilPhysicalProId, null,(string)Session["ExperimentId"]);
                string LiquidCompId = objService.GetLiquidCompId("oil_phys_fk", OilPhysicalProId);
                Session["LiquidCompId"] = LiquidCompId;
            }
        }
    }
    protected void dgOilPhysicalProps_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgOilPhysicalProps.PageIndex = e.NewPageIndex;
        BindDataGrid();
    }
    protected void btnPrevious_Click(object sender, EventArgs e)
    {
        Response.Redirect("Previous Pages/PreOilPhysicalProperties.aspx");
    }
}
